package com.qeegoo.b2bautozityp.moudle.good.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.qeegoo.b2bautozityp.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import base.lib.widget.NonScrollGridView;

/**
 * 王少岩 在 2017/9/21 创建了它
 */

public class CarModelAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<String> mLetter;
    private JSONObject mCarModelList;
    private Fragment mFragment;

    public CarModelAdapter(Fragment fragment, ArrayList<String> letter, JSONObject carModelList) {
        mContext = fragment.getActivity();
        mFragment = fragment;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mLetter = letter;
        mCarModelList = carModelList;
    }

    @Override
    public int getGroupCount() {
        return mLetter.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mLetter.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        Object o = null;
        try {
            o = getChildrenArray(groupPosition).get(childPosition);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return o;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new GroupViewHolder();
            convertView = mInflater.inflate(R.layout.adapter_goods_carmodel_header, null);
            convertView.setOnClickListener(v -> {
            });
            viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.tv_header);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }

        String title = (String) getGroup(groupPosition);
        viewHolder.tvLetter.setText(title);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ChildViewHolder();
            convertView = mInflater.inflate(R.layout.adapter_goods_carmodel_item, null);
            viewHolder.gridView = (NonScrollGridView) convertView.findViewById(R.id.gridview);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }

        CarModelGridAdapter gridAdapter = new CarModelGridAdapter(mFragment, getChildrenArray(groupPosition));
        viewHolder.gridView.setAdapter(gridAdapter);
        viewHolder.gridView.setOnItemClickListener(gridAdapter);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private JSONArray getChildrenArray(int groupPosition) {
        String key = mLetter.get(groupPosition);
        try {
            JSONArray array1 = mCarModelList.getJSONObject("carLogoList").getJSONArray(key);
            return array1;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    class GroupViewHolder {
        TextView tvLetter;
    }

    class ChildViewHolder {
        NonScrollGridView gridView;
    }
}
